package com.smartleanx.module.hrm.service.examine.examineProcess;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.smartleanx.module.hrm.controller.admin.examine.examineProcess.vo.*;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineProcess.ExamineProcessDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.examine.examineProcess.ExamineProcessMapper;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 记录 Service 实现类
 *
 * @author
 */
@Service
@Validated
public class ExamineProcessServiceImpl implements ExamineProcessService {

    @Resource
    private ExamineProcessMapper examineProcessMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Integer createExamineProcess(ExamineProcessSaveReqVO createReqVO) {
        // 插入
        ExamineProcessDO examineProcess = BeanUtils.toBean(createReqVO, ExamineProcessDO.class);
        examineProcessMapper.insert(examineProcess);
        // 返回
        return examineProcess.getId();
    }

    @Override
    public void updateExamineProcess(ExamineProcessSaveReqVO updateReqVO) {
        // 校验存在
        validateExamineProcessExists(updateReqVO.getId());
        // 更新
        ExamineProcessDO updateObj = BeanUtils.toBean(updateReqVO, ExamineProcessDO.class);
        examineProcessMapper.updateById(updateObj);
    }

    @Override
    public void deleteExamineProcess(Integer id) {
        // 校验存在
        validateExamineProcessExists(id);
        // 删除
        examineProcessMapper.deleteById(id);
    }

    private void validateExamineProcessExists(Integer id) {
        if (examineProcessMapper.selectById(id) == null) {
//            throw exception(EXAMINE_PROCESS_NOT_EXISTS);
        }
    }

    @Override
    public ExamineProcessDO getExamineProcess(Integer id) {
        return examineProcessMapper.selectById(id);
    }

    @Override
    public PageResult<ExamineProcessDO> getExamineProcessPage(ExamineProcessPageReqVO pageReqVO) {
        return examineProcessMapper.selectPage(pageReqVO);
    }

    @Override
    public List<JSONObject> selectList(Integer employeeId, Integer examineId) {
        List<ExamineProcessDO> examineProcessDOS = examineProcessMapper.selectList(new LambdaQueryWrapper<ExamineProcessDO>() {{
            eq(ExamineProcessDO::getRecordEmployeeId, employeeId);
            eq(ExamineProcessDO::getExamineId, examineId);
        }});
        List<JSONObject> resultList = new ArrayList<>();
        examineProcessDOS.forEach(examineProcessDO -> {
            JSONObject result = new JSONObject();
            switch (examineProcessDO.getStatus()) {
                case 1:
                    EmployeeDO employeeDO = employeeMapper.selectByUserId(examineProcessDO.getRecordEmployeeId().longValue());
                    result.put("time",examineProcessDO.getCreateTime());
                    result.put("info",employeeDO.getName()+" 提交了评分");
                    result.put("employeeName",employeeDO.getName());
                    resultList.add(result);
                    break;
                case 2:
                    employeeDO = employeeMapper.selectByUserId(examineProcessDO.getReviewEmployeeId().longValue());
                    result.put("time",examineProcessDO.getCreateTime());
                    result.put("info",employeeDO.getName()+" 审核通过了评分");
                    result.put("employeeName",employeeDO.getName());
                    resultList.add(result);

                    break;
                case 3:
                    employeeDO = employeeMapper.selectByUserId(examineProcessDO.getReviewEmployeeId().longValue());
                    result.put("time",examineProcessDO.getCreateTime());
                    result.put("info",employeeDO.getName()+" 驳回了评分");
                    result.put("employeeName",employeeDO.getName());
                    resultList.add(result);
                    break;
                case 4:
                   employeeDO = employeeMapper.selectByUserId(examineProcessDO.getRecordEmployeeId().longValue());
                    result.put("time",examineProcessDO.getCreateTime());
                    result.put("info",employeeDO.getName()+" 申诉了评分");
                    result.put("employeeName",employeeDO.getName());
                    resultList.add(result);
                    break;
                case 5:
                     employeeDO = employeeMapper.selectByUserId(examineProcessDO.getRecordEmployeeId().longValue());
                    result.put("time",examineProcessDO.getCreateTime());
                    result.put("info",employeeDO.getName()+" 确认了评分");
                    result.put("employeeName",employeeDO.getName());
                    resultList.add(result);
                    break;
            }


        });
        return resultList;
    }

}